package net.xju3_4.controller;






import net.xju3_4.common.framework.simplemvc.RequestMapping;
import net.xju3_4.common.utils.JSONUtil;
import net.xju3_4.common.utils.Result;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.UUID;

@RequestMapping("/api/file")
public class FileController {
    private static final String UPLOAD_DIR = "file";

    @RequestMapping("/download.do")
    public void download(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1. 获取文件名
        request.setCharacterEncoding("utf-8");
        String filename = request.getParameter("filename");
//        String uploadDir = request.getServletContext().getRealPath("") + UPLOAD_DIR + File.separator;
        // 1. 获取配置的上传目录
        String uploadDir = "D:\\Upload File";
        System.out.println(uploadDir);
        File file = new File(uploadDir, filename);

        if (!file.exists()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        response.setContentType(request.getServletContext().getMimeType(filename));
        response.setContentLength((int) file.length());

        try (InputStream in = new FileInputStream(file);
             OutputStream out = response.getOutputStream()) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
        }
    }

    @RequestMapping("/upload.do")
    public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 准备上传目录
        String appPath = request.getServletContext().getRealPath("");
//        String uploadPath = appPath + UPLOAD_DIR;
        String uploadPath =  "D:\\Upload File";

        // 如果目录不存在则创建
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            boolean mkdir = uploadDir.mkdir();
            System.out.println(uploadDir.getPath() + "创建结果：" + mkdir);
        }

        try {
            // 获取上传的文件
            Part filePart = request.getPart("file");
            String fileName = filePart.getSubmittedFileName();

            // 生成唯一文件名
            String fileExtension = fileName.substring(fileName.lastIndexOf("."));
            String uniqueFileName = UUID.randomUUID() + fileExtension;

            // 保存文件
            String filePath = uploadPath + File.separator + uniqueFileName;
            try (InputStream fileContent = filePart.getInputStream()) {
                Files.copy(fileContent, Paths.get(filePath), StandardCopyOption.REPLACE_EXISTING);
            }

           